import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('views/home/Home')
const Category = () => import('views/category/Category')
const Cart = () => import('views/cart/Cart')
const Profile = () => import('views/profile/Profile')

// 解决在VUE中路由遇到Error: Avoided redundant navigation to current location:报错显示是路由重复,这是多次访问路由形成的
const originalPush = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [{
    path: "",
    meta: {
      title: '主页'
    },
    redirect: "/home"
  },
  {
    path: '/home',
    meta: {
      title: '主页'
    },
    component: Home
  },
  {
    path: '/Category',
    meta: {
      title: '分类1'
    },
    component: Category
  },
  {
    path: '/cart',
    meta: {
      title: '分类2'
    },
    component: Cart
  },
  {
    path: "/profile",
    meta: {
      title: '分类3'
    },
    component: Profile
  }
]

const router = new VueRouter({
  routes,
  mode: "history"
})
// 使用全局导航守卫：监听路由跳转对页面进行操作
// 前置守卫（前置钩子）跳转之后回调
router.beforeEach((to, from, next) => {
  // 从from跳转到to
  /* 对页面的title进行操作 */
  document.title = to.matched[0].meta.title;
  // console.log(to);
  /* next()必须调用，否则不会往下执行可以进行路由跳转=》传入路由（可以再路由独享守卫中进行灵活使用） */
  next()
})

export default router